package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.table.GoodsTable;
import com.cjack.frenchexcel.table.SendGoodsRecordTable;
import com.cjack.frenchexcel.util.*;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.MiniTableRenderData;
import com.deepoove.poi.data.RowRenderData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class WordPdfCreateService {
    private final static Logger LOG = LogManager.getLogger( WordPdfCreateService.class);

    @Autowired
    ConsumerRecordService consumerRecordService;

    @Value("${file.upload.baseServerDir}")
    String baseServerDir;
    @Value("${file.upload.baseClientDir}")
    String baseClientDir;
    @Value("${file.upload.spelitor}")
    String spelitor;
    @Value("${invoice.templete}")
    String templete;

    /**
     * 创建商品发货的发票
     * @return
     */
    public String createSendGoodsInvoice(List<GoodsTable> tables, SendGoodsRecordTable sendGoodsRecordTable){
        String wordFileClientPath = "";
        try{
            String dumic = spelitor + "sellInvoice" + spelitor + FileUtils.getDirByDate();//动态的这一截路径
            String fileName = FileUtils.getFileName( "out_template.docx");
            //服务器保存文件的目录
            String destDir = baseServerDir + dumic;
            //供浏览器客户端访问的目录
            wordFileClientPath = baseClientDir + dumic + spelitor + fileName;
            //先判断当前的目录是否存在
            FileUtils.handleDir( destDir);
            String wordFilePath = destDir + spelitor + fileName;

            Map<String,Object> params = handleWordParams( tables, sendGoodsRecordTable);
            XWPFTemplate template = XWPFTemplate.compile( templete).render( params);
            FileOutputStream out = new FileOutputStream( wordFilePath);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        }catch ( Exception e){
            e.printStackTrace();
            LOG.error( e);
        }
        return wordFileClientPath;
    }

    //处理需要动态的参数
    public Map<String,Object> handleWordParams( List<GoodsTable> goods, SendGoodsRecordTable sendGoodsRecordTable){
        DecimalFormat df =new DecimalFormat("#.00");

        Map<String,Object> params = new HashMap<>();

        params.put( "sendGoodsTime", DateFormatUtil.format( sendGoodsRecordTable.getCreateTime(), DateFormatUtil.DATE_RULE_7));//18-11-2019这种格式
        //这个参数用前后两段组合
        params.put( "sendGoodsNumber", sendGoodsRecordTable.getCompleteSendGoodsNoNumber());

        if( false){
            params.put( "companyName", "");
            params.put( "addr", "");
            params.put( "emailCode", "");
            params.put( "city", "");
            params.put( "country", "");
        }

        //商品的数据
        //表头
        RowRenderData header = RowRenderData.build( "CODE", "DECRIPTION", "Qté", "HT €", "TOL HT €");
        List< RowRenderData> rows = new ArrayList<>();
        BigDecimal totalBg = new BigDecimal( 0);
        for( GoodsTable good : goods){
            //数据行 商品编号 商品名 商品数量 金额1 金额2
            RowRenderData row = RowRenderData.build( good.getNumber(),  good.getName(),
                    "1", df.format( good.getSellPrice()) + "", df.format( good.getSellPrice()) + "");
            rows.add( row);
            totalBg = totalBg.add( new BigDecimal( good.getSellPrice()));
        }

        //总额
        params.put( "total", df.format( totalBg));
        //商品编号 商品名 商品数量 金额1 金额2
        params.put("sendGoodsTable", new MiniTableRenderData( header, rows, 16.35f));
        return params;
    }
}
